package com.paul.util;

import java.sql.Timestamp;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jnu.common.constant.DateConstant;


public class DateUtil {
	private static final Log log = LogFactory.getLog(DateUtil.class);

	public static void main(String[] args) {
		//		Calendar calendar = Calendar.getInstance();
		//		calendar.set(2006, 0, 1);
		//		calendar.add(Calendar.DAY_OF_YEAR, -1);
		//		log.debug(toString(calendar.getTime(), DateConstant.CHS_DATE_FORMAT_MASK));
		String txnTime = "10-1-2006 23:10:10";
		log.debug(Timestamp.valueOf(txnTime));
	}

	public static int getCurrentYear() {
		return Calendar.getInstance().get(Calendar.YEAR);
	}

	public static java.util.Date getCurrentUtilDate() {
		return Calendar.getInstance().getTime();
	}

	public static java.sql.Date getCurrentSqlDate() {
		return new java.sql.Date(getCurrentUtilDate().getTime());
	}

	public static java.sql.Timestamp getCurrentTimestamp() {
		return new java.sql.Timestamp(getCurrentUtilDate().getTime());
	}

	public static java.sql.Timestamp utilDateToTimestamp(java.util.Date date) {
		if (date != null) {
			return new java.sql.Timestamp(date.getTime());
		}
		return null;
	}

	public static java.util.Date timestampToUtilDate(java.sql.Timestamp timestamp) {
		if (timestamp != null) {
			return new java.util.Date(timestamp.getTime());
		}
		return null;
	}

	public static java.sql.Date utilDateToSqlDate(java.util.Date date) {
		if (date != null) {
			return new java.sql.Date(date.getTime());
		}
		return null;
	}

	public static boolean isDateFormat(String strDate, String pattern) {
		return strToUtilDate(strDate, pattern) != null;
	}

	public static java.util.Date strToUtilDate(String strDate) {

		return strToUtilDate(strDate, DateConstant.CHS_DATETIME_FORMAT_MASK);
	}

	public static java.util.Date strToUtilDate(String strDate, String pattern) {
		if (!StringUtil.isEmpty(strDate)) {
			SimpleDateFormat smf = new SimpleDateFormat(pattern);
			ParsePosition pos = new ParsePosition(0);
			smf.setLenient(false);
			return smf.parse(strDate, pos);
		}
		return null;
	}

	public static java.sql.Date strToSqlDate(String strDate) {
		return utilDateToSqlDate(strToUtilDate(strDate, DateConstant.CHS_DATE_FORMAT_MASK));

	}

	public static java.sql.Date strToSqlDate(String strDate, String pattern) {
		return utilDateToSqlDate(strToUtilDate(strDate, pattern));

	}

	public static Timestamp strToTimestamp(String strDate) {
		java.util.Date date = strToUtilDate(strDate);
		return utilDateToTimestamp(date);
	}

	public static Timestamp strToTimestamp(String strDate, String pattern) {
		java.util.Date date = strToUtilDate(strDate, pattern);
		return utilDateToTimestamp(date);
	}

	public static boolean isWeekEnd(java.util.Date strDate) {
		if (strDate == null)
			return false;
		boolean bRtn = false;
		int day_of_week = 1;
		Calendar cal = Calendar.getInstance();

		cal.setTime(strDate);
		day_of_week = cal.get(Calendar.DAY_OF_WEEK);
		if (day_of_week == Calendar.SUNDAY || day_of_week == Calendar.SATURDAY) {
			bRtn = true;
		}
		return bRtn;

	}

	public static int getLastDayOfMonth(java.util.Date date) {
		int lastDay;
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		Timestamp timestamp = utilDateToTimestamp(date);
		int month = timestamp.getMonth();
		boolean isLeapYear = ((GregorianCalendar) cal).isLeapYear(timestamp.getYear());

		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
			lastDay = 31;
		} else if (month == 4 || month == 6 || month == 9 || month == 11) {
			lastDay = 31;
		} else if (month == 2 && isLeapYear) {
			lastDay = 29;
		} else {
			lastDay = 28;
		}
		return lastDay;

	}

	public static final Timestamp getTimeAfter(Timestamp date, int field, int amount) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(field, amount);
		return utilDateToTimestamp(cal.getTime());
	}

	public static final Timestamp getTimeBefore(Timestamp date, int field, int amount) {

		return getTimeAfter(date, field, -amount);
	}

	public static final java.sql.Date getDateAfter(java.sql.Date date, int field, int amount) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(field, amount);
		return utilDateToSqlDate(cal.getTime());
	}

	public static final java.sql.Date getDateBefore(java.sql.Date date, int field, int amount) {
		return getDateAfter(date, field, -amount);
	}

	public static final java.sql.Date getNextDay(java.sql.Date date) {
		return getDateAfter(date, Calendar.DAY_OF_YEAR, 1);
	}

	public static final java.sql.Date getPreviousDay(java.sql.Date date) {
		return getDateBefore(date, Calendar.DAY_OF_YEAR, 1);
	}

	public static final java.util.Date getDate(java.util.Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.AM_PM, Calendar.AM);
		cal.set(Calendar.HOUR, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	public static final java.sql.Timestamp getDate(java.sql.Timestamp origTs) {
		java.sql.Timestamp newTs = null;
		Calendar c = Calendar.getInstance();
		c.setTime(origTs);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.MILLISECOND, 0);
		newTs = new java.sql.Timestamp(c.getTime().getTime());
		return newTs;
	}

	public static String toString(java.sql.Timestamp timestamp, String pattern) {
		if (StringUtil.isEmpty(pattern))
			pattern = DateConstant.CHS_DATE_FORMAT_MASK;
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		String dateString = null;
		if (timestamp != null)
			dateString = sdf.format(timestamp);

		return dateString;
	}

	public static String toString(java.util.Date date, String pattern) {
		if (StringUtil.isEmpty(pattern))
			pattern = DateConstant.CHS_DATE_FORMAT_MASK;
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		String dateString = null;
		if (date != null)
			dateString = sdf.format(date);

		return dateString;
	}

}